//
//  ViewController.m
//  CircleAccountBooks0
//
//  Created by ake on 16/4/5.
//  Copyright © 2016年 ake. All rights reserved.
//

#import "YZMainViewController.h"
#import "YZMenuView.h"
#import "YZWeatherView.h"
#import "YZProvinceController.h"
#import "YZWeather.h"
#import "YZSaveDataTool.h"
#import <SVProgressHUD.h>
#import "YZLoadWeatherViewModel.h"
#import "YZDetailsOfDisease.h"
#import "YZGongyiwebController.h"
#import "AppDelegate.h"
#import <Masonry/Masonry.h>

@interface YZMainViewController ()<YZWeatherViewDelegate>
@property (nonatomic,strong)YZWeatherView *weatherView;



@end

@implementation YZMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.weatherView = [YZWeatherView weatherView];
    self.weatherView.delegate = self;
    [self.homeView addSubview:self.weatherView];
    self.weatherView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 70.5);
    //接收到改变天气通知

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeWeatherInfo:) name:YZWeatherNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(cantFindWeather) name:YZErrorWeather object:nil];
    
    //从偏好设置读取上次保存的城市
    
    
    NSString *city = [YZSaveDataTool valueForKey:@"city"];
    if (city.length != 0) {
        
        [self defaultCityWeather:city];
    }else{
        city = @"请选择城市";
        YZWeather *weather = [[YZWeather alloc]init];
        weather.city = city;
        weather.weather = @"";
        weather.temperature = @"";
        NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
        formatter2.dateFormat = @"yyyy年MM月dd日";
        NSString *date2 = [formatter2 stringFromDate:[NSDate date]];
        weather.date = date2;
        self.weatherView.weather = weather;
    }
  
    

}
- (IBAction)gongyi {
    [self.navigationController pushViewController:[[YZGongyiwebController alloc]init]animated:YES];
    
}

- (IBAction)menuBtnDidClick:(id)sender {
    self.menuBtnBlock();
}
-(void)defaultCityWeather:(NSString *)city{
    if ([city isEqualToString:@"请选择城市"]) {
        return;
    }
    
    [YZLoadWeatherViewModel loadWeather:city];

}


-(void)weatherViewButtonClick:(UIButton *)btn{
    UIViewController *pvc = [[YZProvinceController alloc]init];
    [self.navigationController pushViewController:pvc animated:YES];
}

-(void)changeWeatherInfo:(NSNotification *)notification{
    NSDictionary *weatherInfo = notification.userInfo;
    
    YZWeather *weather = [[YZWeather alloc]init];
    weather.temperature = weatherInfo[@"temperature"];
    weather.weather = weatherInfo[@"weather"];
    weather.city = weatherInfo[@"city"];
    NSString *date = weatherInfo[@"date"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSDate *today = [formatter dateFromString:date];
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
    formatter2.dateFormat = @"yyyy年MM月dd日";
    NSString *date2 = [formatter2 stringFromDate:today];
    weather.date = date2;
    

    [YZSaveDataTool setValue:weather.city forKey:@"city"];



    
    self.weatherView.weather = weather;
}
-(void)cantFindWeather{
    NSLog(@"度娘没有收录此地天气信息!");
    
    [SVProgressHUD showInfoWithStatus:@"度娘没有收录此地天气信息!"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [SVProgressHUD dismiss];
    });
}

-(void)viewWillAppear:(BOOL)animated{
    
     self.navigationController.navigationBarHidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}


-(IBAction)fiveButtonDidClick:(UIButton *)sender{
    
    
    YZDetailsOfDisease *detailsVC = [YZDetailsOfDisease detailOfDiseaseVCWithId:sender.tag];
    [self.navigationController pushViewController:detailsVC animated:YES];
    
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
